home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / regdat / regdat.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-05-07  |  6.1 KB  |  125 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "File Association Example"
  4.    ClientHeight    =   1470
  5.    ClientLeft      =   1695
  6.    ClientTop       =   3390
  7.    ClientWidth     =   5430
  8.    Height          =   1875
  9.    Icon            =   0
  10.    Left            =   1635
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   1470
  13.    ScaleWidth      =   5430
  14.    Top             =   3045
  15.    Width           =   5550
  16.    Begin Label Label2 
  17.       Height          =   315
  18.       Left            =   480
  19.       TabIndex        =   1
  20.       Top             =   285
  21.       Width           =   4440
  22.    End
  23.    Begin Label Label1 
  24.       BorderStyle     =   1  'Fixed Single
  25.       Height          =   285
  26.       Left            =   420
  27.       TabIndex        =   0
  28.       Top             =   870
  29.       Width           =   4560
  30.    End
  31. DefInt A-Z
  32. ' Registration Database (REG.DAT) functions.
  33. ' (Requires Windows 3.1+, or 3.0 with SHELL.DLL.)
  34. Declare Function RegCloseKey& Lib "Shell.dll" (ByVal hkey&)
  35. Declare Function RegCreateKey& Lib "Shell.dll" (ByVal hkey&, ByVal lpszSubKey$, lphKey&)
  36. Declare Function RegDeleteKey& Lib "Shell.dll" (ByVal hkey&, ByVal lpszSubKey$)
  37. Declare Function RegOpenKey& Lib "Shell.dll" (ByVal hkey&, ByVal lpszSubKey$, lphKey&)
  38. Declare Function RegQueryValue& Lib "Shell.dll" (ByVal hkey&, ByVal lpszSubKey$, ByVal lpszValue$, dwLength&)
  39. Declare Function RegSetValue& Lib "Shell.dll" (ByVal hkey&, ByVal lpszSubKey$, ByVal fdwType&, ByVal lpszValue$, ByVal dwLength&)
  40. ' Private initialization file (.INI) functions.
  41. Declare Function GetPrivateProfileString% Lib "Kernel" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpRetString$, ByVal nSize%, ByVal lpFileName$)
  42. Declare Function WritePrivateProfileString% Lib "Kernel" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpString As Any, ByVal lpFileName$)
  43. ''' Written by Lou A. Moccia (thescree@aol.com), 10/94.
  44. ''' Released "as-is" and shared freely with all fellow
  45. ''' VB-WIN programmers.
  46. ''' The REG.DAT code presented here was worked out through
  47. ''' a bit of trial-and-error, because I could not find this
  48. ''' info for VB anywhere, not even that popular "guide to
  49. ''' the Windows API" book for VB.  I do have to give some
  50. ''' credit to the Windows SDK Help file included with VB3,
  51. ''' however shame on Microsoft for only providing dreaded
  52. ''' C examples!
  53. ''' Anyway, I hope you find this code useful!
  54. Sub Form_Load ()
  55. Extension$ = "TXT"
  56. Label2.Caption = "Current association for """ & Extension$ & """ files:"
  57. Label1.Caption = Get_Association$(Extension$)
  58. End Sub
  59. '--------------------------------------------------------------------------------------
  60. ' Returns the association for the specified file extension. The Registration
  61. ' Database (REG.DAT) file is accessed first by a file utility like File Manager
  62. ' when it attempts to launch a document file, so here REG.DAT is checked for the
  63. ' association first. If nothing is found there, then the [Extensions] section
  64. ' of the WIN.INI file is checked.
  65. ' Extension$ = The file extension to check for an association.
  66. '              Sample form:  "txt"
  67. '              Do not include a period before the extension.
  68. ' Returns:  The full association string, or an empty string if not found.
  69. '           Sample return from REG.DAT:  "c:\windows\notepad.exe %1"
  70. '           Sample return from WIN.INI:  "c:\windows\notepad.exe ^.txt"
  71. '           It will be up to you to parse off the %1 or ^.xxx part if desired.
  72. '--------------------------------------------------------------------------------------
  73. Function Get_Association$ (Extension$)
  74. Get_Association$ = ""
  75. Ext$ = "." & Extension$
  76. tmp$ = Space$(256)
  77. cb& = Len(tmp$)
  78. On Error Resume Next
  79. r& = RegOpenKey(1, Ext$, lphKey&)
  80. If r& = 0& Then
  81.     r1& = RegQueryValue(lphKey&, "shell\open\command", tmp$, cb&)
  82.     ' If no error and the length of the returned string is greater than 1 (a null
  83.     ' is always returned), get the string up to the null and trim it.
  84.     If r1& = 0& And cb& > 1 Then Get_Association$ = Trim$(Left$(tmp$, cb& - 1))
  85.     r1& = RegCloseKey(lphKey&)
  86.     ret% = GetPrivateProfileString("Extensions", Extension$, "", tmp$, Len(tmp$), "WIN.INI")
  87.     ' If the returned string is greater than 5 (the end part ^.xxx),
  88.     ' get the string and trim it.
  89.     If ret% > 5 Then Get_Association$ = Trim$(Left$(tmp$, ret%))
  90. End If
  91. End Function
  92. '--------------------------------------------------------------------------------------
  93. ' Updates an association in the Registration Database (REG.DAT) file and
  94. ' in the [Extensions] section of the WIN.INI file for the specified file
  95. ' extension, just like File Manager does.
  96. ' Extension$ = The file extension to associate or disassociate.
  97. '              Sample form:  "txt"
  98. '              Do not include a period before the extension.
  99. ' Application$ = To associate, the application command.
  100. '                Sample form:  "c:\windows\notepad.exe"
  101. '                To disassociate, an empty string.
  102. ' Description$ = Optional description of the file extension, used
  103. '                in REG.DAT. Really only useful to yourself.
  104. '                Sample text:  "Text Files"
  105. '                Empty string for no description.
  106. ' If does not matter if the association already exists or not.
  107. '--------------------------------------------------------------------------------------
  108. Sub Set_Association (Extension$, Application$, Description$)
  109. Ext$ = "." & Extension$
  110. If Len(Application$) = 0 Then
  111.     ret% = WritePrivateProfileString("Extensions", Extension$, 0&, "WIN.INI")
  112.     r& = RegOpenKey(1, "", lphKey&)
  113.     If r& = 0& Then r1& = RegDeleteKey(lphKey&, Ext$)
  114.     Association$ = Application$ & " ^." & Extension$
  115.     ret% = WritePrivateProfileString("Extensions", Extension$, Association$, "WIN.INI")
  116.     r& = RegCreateKey(1, Ext$, lphKey&)
  117.     If r& = 0& Then
  118.         r1& = RegSetValue(lphKey&, "", 1, Description$, 0&)
  119.         Association$ = Application$ & " %1"
  120.         r1& = RegSetValue(lphKey&, "shell\open\command", 1, Association$, 256&)
  121.     End If
  122. End If
  123. If r& = 0& Then r1& = RegCloseKey(lphKey&)
  124. End Sub
  125.